//
//  MyPrivateMessageListVC.m
//  OnionHome
//
//  Created by fengjie on 2019/10/14.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "MyPrivateMessageListVC.h"
#import "MyPrivateMessageChatRoomVC.h"

@interface MyPrivateMessageListVC ()

/**listViewTool*/
@property (nonatomic,strong)UITableViewTool*listViewTool;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataSoure;

@end

@implementation MyPrivateMessageListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的私信";
    [self createUI];
}

-(void)createUI{
    [self.listViewTool.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
#pragma mark 懒加载
-(UITableViewTool*)listViewTool{
    if(!_listViewTool){
        kWeakSelf(weakSelf);
        _listViewTool=[[UITableViewTool alloc] initWithFrame:CGRectZero style:UITableViewStylePlain registerCellClassName:NSStringFromClass([MyPrivateMessageListCell class]) identifier:NSStringFromClass([MyPrivateMessageListCell class]) dataArray:self.dataSoure delegate:self dataSourceBlock:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            MyPrivateMessageListCell * cell = (MyPrivateMessageListCell *)TempCell;
            cell.model = (MyPrivateMessageModel *)TempData;
            
        } selectCell:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            MyPrivateMessageChatRoomVC * vc = [MyPrivateMessageChatRoomVC new];
            vc.model = (MyPrivateMessageModel *)TempData;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        _listViewTool.listView.backgroundColor = RGB(236, 239, 243);
        [_listViewTool.listView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    return _listViewTool;
}
-(NSMutableArray *)dataSoure{
    if (!_dataSoure) {
        _dataSoure =[NSMutableArray array];
        _dataSoure =[NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPrivateMessageList" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MyPrivateMessageModel *model = [MyPrivateMessageModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataSoure addObject:model];
        }];
    }
    return _dataSoure;
}
@end

@interface MyPrivateMessageListCell ()
/**<#属性#>*/
@property (nonatomic,strong)UserIconAndNameView*userView;
/**<#属性#>*/
@property (nonatomic,strong)UILabel*messageLabel;
/**<#属性#>*/
@property (nonatomic,strong)UILabel*timerLabel;

@end

@implementation MyPrivateMessageListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setModel:(MyPrivateMessageModel *)model{
    _model = model;
    self.userView.userName.text = model.userName;
    self.userView.iconImage.image = IMAGE_NAMED(model.userIcon);
    MyPrivateMessageModel * meseModel = model.modelArray.firstObject;
//    self.messageLabel.text = meseModel.message;
    self.messageLabel.attributedText = [TitleTool getAttributeStringLineSpacing:4 title:meseModel.message titleColor:UIColorFromRGB(0x273323) titleFont:SYSTEMFONT(14)];
}

-(void)createUI{
    [self addSubview:self.userView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.timerLabel];
    
    [self.userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(68);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.userView.mas_bottom);
    }];
    [self.timerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.messageLabel);
        make.top.mas_equalTo(self.messageLabel.mas_bottom).offset(8);
        make.bottom.mas_equalTo(-16);
    }];
}
-(UserIconAndNameView*)userView{
    if(!_userView){
        _userView=[UserIconAndNameView new];
        _userView.deleButton.hidden = YES;
        _userView.dynamicData.text = @"美食达人";
    }
    return _userView;
}
-(UILabel*)messageLabel{
    if(!_messageLabel){
        _messageLabel=[UILabel new];
        _messageLabel.textColor = UIColorFromRGB(0x273323);
        _messageLabel.font = SYSTEMFONT(14);
        _messageLabel.numberOfLines = 2;
    }
    return _messageLabel;
}
-(UILabel*)timerLabel{
    if(!_timerLabel){
        _timerLabel=[UILabel new];
        _timerLabel.textColor = UIColorFromRGB(0x454F44);
        _timerLabel.font = SYSTEMFONT(12);
        _timerLabel.text = @"04-19 15:30";
    }
    return _timerLabel;
}
@end
